package org.mvnsearch.rsocket.responder;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.rsocket.messaging.RSocketStrategiesCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.http.codec.protobuf.ProtobufDecoder;
import org.springframework.http.codec.protobuf.ProtobufEncoder;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.blackbird.BlackbirdModule;

/**
 * rsocket responder app
 *
 * @author linux_china
 */
@SpringBootApplication
public class RSocketResponderApp {
	private static final MediaType[] SUPPORTED_TYPES = { MediaType.APPLICATION_JSON,
			new MediaType("application", "*+json") };
	
	@Bean("protoRSocketStrategies")
	@Order(1)
	public RSocketStrategiesCustomizer protoBufRSocketStrategyCustomizer(ObjectMapper objectMapper) {
		return (strategy) -> {
			strategy.decoder(new ProtobufDecoder(),new Jackson2JsonDecoder(objectMapper, SUPPORTED_TYPES));
			strategy.encoder(new ProtobufEncoder(),new Jackson2JsonEncoder(objectMapper, SUPPORTED_TYPES));
			
		};
	}
	
	@Bean
	BlackbirdModule blackbirdModule() {
		BlackbirdModule blackbird = new BlackbirdModule();
		return blackbird;
	}
	
	public static void main(String[] args) {
        SpringApplication.run(RSocketResponderApp.class, args);
    }
}
